package main.gfs;

import model.gfs.Ticket;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CommentTicketActivity extends Activity {
	private Ticket oldticket;
	private EditText editAnswer;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_ticket);
		editAnswer = (EditText) findViewById(R.id.editAnswer);
		oldticket = getIntent().getExtras().getParcelable("oldticket");
	}
	
	/**
	 * Щелчек по кнопке Отправить
	 * он вызывается сам потому как привязан в сщььуте_ticket.xml 
	 * по свойству onClick
	 * @param v
	 */
	public void btnSend_Click(View v){
		Ticket newticket = new Ticket();
		String type = "comment";
		
		if(oldticket.getType().equals("forum"))
			newticket.setPid(oldticket.getId());
		if(oldticket.getType().equals("comment"))
			newticket.setPid(oldticket.getPid());
		if(oldticket.getType().equals("blog")){
			if(oldticket.getPid()>0){
				newticket.setPid(oldticket.getPid());
			}else{
				newticket.setPid(oldticket.getId());
			}
			type = "blog";
		}
		newticket.setTxt(editAnswer.getText().toString());
		newticket.setType(type);
		if(newticket.sendToServer(this)){
			sendBroadcast(new Intent("main.gfs.Update"));
			finish();
		}else{
			Toast.makeText(this, "Ошибка отправки данных", 50000).show();
		}
	}
	
	public void btnCancel_Click(View v){
		finish();
	}
}
